package mspbots.data.tl.prod.autotask.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import mspbots.data.tl.base.AbstractBaseServiceImpl;
import mspbots.data.tl.entity.ResultEntity;
import mspbots.data.tl.prod.autotask.mapper.TaskAutotaskInformationMapper;
import mspbots.data.tl.prod.autotask.mapper.TaskAutotaskTicketMapper;
import mspbots.data.tl.prod.autotask.model.TaskAutotaskInformation;
import mspbots.data.tl.prod.autotask.model.TaskAutotaskTicket;
import mspbots.data.tl.prod.autotask.service.TaskAutotaskInformationService;
import mspbots.data.tl.prod.autotask.service.TaskAutotaskTicketService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author Jay.Yang
 */

@Service
@Log4j2
@RequiredArgsConstructor
@DS("master")
public class TaskAutotaskInformationServiceImpl extends AbstractBaseServiceImpl<TaskAutotaskInformationMapper, TaskAutotaskInformation>
        implements TaskAutotaskInformationService {

    private final TaskAutotaskTicketMapper taskAutotaskTicketMapper;
    private final TaskAutotaskTicketService taskAutotaskTicketService;

    @Override
    public void sync(ResultEntity content) {
        log.debug("Auto task information receiver content {}", content);
        JsonNode entity = content.getEntity();
        if (ObjectUtils.isEmpty(entity.get("name"))){
            log.error("Auto task information receiver error,name is null!");
            return;
        }
        String name = entity.get("name").asText();
        if (ObjectUtils.isEmpty(entity.get("picklistValues")) || entity.get("picklistValues").isEmpty()){
            log.debug("Auto task information receiver error,picklistValues is null!  name:{}!",name);
            return;
        }
        Iterator<JsonNode> picklistValues = entity.get("picklistValues").elements();
        while (picklistValues.hasNext()){
            try {
                JsonNode next = picklistValues.next();
                TaskAutotaskInformation data = TaskAutotaskInformation.builder()
                        .type(content.getAction())
                        .name(name)
                        .label(ObjectUtils.isEmpty(next.get("label")) ? null : next.get("label").asText())
                        .value(ObjectUtils.isEmpty(next.get("value")) ? null : next.get("value").asText())
                        .parentValue(ObjectUtils.isEmpty(next.get("parentValue")) ? null : next.get("parentValue").asText())
                        .isActive(ObjectUtils.isEmpty(next.get("isActive")) ? null : next.get("isActive").asBoolean())
                        .isSystem(ObjectUtils.isEmpty(next.get("isSystem")) ? null : next.get("isSystem").asBoolean())
                        .isDefaultValue(ObjectUtils.isEmpty(next.get("isDefaultValue")) ? null : next.get("isDefaultValue").asBoolean())
                        .sortOrder(ObjectUtils.isEmpty(next.get("sortOrder")) ? null : next.get("sortOrder").asInt())
                        .syncTime(LocalDateTime.now())
                        .build();
                data.setTenantCode(content.getTenantId());

                TaskAutotaskInformation dbData = super.baseMapper.selectOne(Wrappers.<TaskAutotaskInformation>lambdaQuery()
                        .eq(TaskAutotaskInformation::getTenantCode, data.getTenantCode())
                        .eq(TaskAutotaskInformation::getName, data.getName())
                        .eq(TaskAutotaskInformation::getValue, data.getValue())
                        .eq(TaskAutotaskInformation::getType,data.getType())
                        .last("limit 1"));
                if (ObjectUtils.isEmpty(dbData)) {
                    super.baseMapper.insert(data);
                } else {
                    data.setId(dbData.getId());
                    super.baseMapper.updateById(data);
                }
            } catch (Exception e) {
                log.error(e.getMessage());
            }
        }

    }

}
